﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DataAccess.SingleSignOn;
using Single_SignOn.Areas.PDA.Models;
using Single_SignOn.Models;
using Single_SignOn.ResX;

namespace Single_SignOn.Areas.PDA.Controllers
{
    public class FunctionListController : Controller
    {
        //
        // GET: /IE5/FunctionList/


        public AuthenConnection oAuth;

        public FunctionListModels _model;

        public FunctionListModels _functionmodel;
        //
        // GET: /Handheld/FunctionList/

        public ActionResult Index()
        {
            oAuth = new AuthenConnection();
            string company = "";
            string language = "";
            string applno = "";
            string companyName = "";
            FunctionListModels models = null;

            if (Request.QueryString["token"] == null)
            {
                company = Request.QueryString["comp"];
                language = Request.QueryString["lang"];
                applno = Request.QueryString["appl"];
                companyName = Convert.ToString(oAuth.getCompanyName(company).Rows[0]["company_name_en"]);
            }
            else
            {
                string token = (string)Request.QueryString["token"];
                // Get Token
                DataTable dtToken = oAuth.getAppTokenData(token);
                // get Session
                SessionPool.getSession(Convert.ToString(dtToken.Rows[0]["EMPNO"]));
                company = Convert.ToString(dtToken.Rows[0]["COMPANY_CODE"]);
                language = Convert.ToString(dtToken.Rows[0]["LANG"]);
                applno = Convert.ToString(dtToken.Rows[0]["APPLNO"]);
                companyName = Convert.ToString(oAuth.getCompanyName(company).Rows[0]["company_name_en"]);
            }

            ViewBag.Company = companyName;

            ViewBag.CompanyName = company;

            ViewBag.Language = language;

            ViewBag.ApplNo = applno;

            ViewBag.FunctionList = oAuth.getApplicationListByApplNo(((UserProfile)Session["UserInfo"]).Username
                    , company,
                    applno).Rows[0]["description"];

            models = GetFunctionList(applno, company);

            return View("Index", models);
        }

        public ActionResult GetToken(string applno, string funcno, string company, string language)
        {

            oAuth = new AuthenConnection();

            String token = Guid.NewGuid().ToString();

            UserProfile o = (UserProfile)Session["UserInfo"];

            String group = oAuth.getGroup(o.Username, applno,
                company);

            DataTable dt = oAuth.getFunctionListByApplNoWithHandHeld(o.Username, company, group, applno,
                funcno);

            string url = (String)dt.Rows[0]["function_url"];

            string result = oAuth.insertTokenApp(o.Username, o.Password, token, applno,
                (String)oAuth.getApplicationListByApplNo(o.Username, company, applno).Rows[0]["applname"]
                , group, o.Division, language, company, funcno);

            // Updated Current Language CurrentUI
            oAuth.upUserCurrentUI(o.Username, language, company);

            return Redirect(string.Format("{0}?token={1}"
                    , url
                    , result));
        }

        protected FunctionListModels GetFunctionList(String applno, String comp)
        {
            _functionmodel = new FunctionListModels();

            String group = oAuth.getGroup(((UserProfile)Session["UserInfo"]).Username, applno,
                comp);

            //get Application List
            DataTable dtApplication =
                oAuth.getFunctionListWithHandHeld((string)((UserProfile)Session["UserInfo"]).Username, comp, group, applno);

            FunctionList function;
            if (dtApplication.Rows.Count > 0)
            {
                List<FunctionList> functionListsLists = new List<FunctionList>();
                for (int i = 0; i < dtApplication.Rows.Count; i++)
                {
                    function = new FunctionList();
                    function.No = i + 1;
                    function.FuncNo = (string)dtApplication.Rows[i]["function_no"];
                    function.Application = (string)dtApplication.Rows[i]["function_name"];
                    function.Description = (string)dtApplication.Rows[i]["function_name"];
                    functionListsLists.Add(function);
                }
                _functionmodel.FunctionLists = functionListsLists;
            }
            return _functionmodel;
        }

    }
}
